//
//  MyClass.m
//  PushInappTestiOS
//
//  Created by KyuJin on 12. 12. 18..
//
//

#import "BridgePlugin.h"
#import "MainViewController.h"
#import "InAppPurchase.h"

@implementation BridgePlugin

#pragma mark - Receive Function (JS -> Native)
- (void) network:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
    NSString* message = [command.arguments objectAtIndex:0];
    
    if (message != nil && [message length] > 0)
    {
        //이벤트 발생 시 네이티브에서 처리해야 할 로직을 구현해야합니다.
        if([message isEqualToString:@"online"])
        {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
        }
        else if([message isEqualToString:@"offline"])
        {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
        }
    }
    else
    {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }
    
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    
    
    // Threading !
    //    // Check command.arguments here.
    //    [self.commandDelegate runInBackground:^{
    //        NSString* payload = nil;
    //        // Some blocking logic...
    //        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:payload];
    //        // The sendPluginResult method is thread-safe.
    //        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    //    }];
}

- (void) login:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
    NSString* message = [command.arguments objectAtIndex:0];
    
    if (message != nil && [message length] > 0)
    {
        //이벤트 발생 시 처리해야할 로직을 구현해야합니다.
        if([message isEqualToString:@"success"])
        {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
        }
        else if([message isEqualToString:@"fail"])
        {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
        }
        else
        {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
        }
    }
    else
    {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }
    
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) changemenu:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
    NSString* message = [command.arguments objectAtIndex:0];
 
    //메뉴 버튼 이미지를 전환하기 위한 로직이 필요합니다.
    //UI는 메인 스레드에서 작업을 해야하므로 이 점에 유의할 것
    if (message != nil && [message length] > 0)
    {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
        [((MainViewController*)self.viewController) controlButtonImage:message.integerValue];
    }
    else
    {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }
    
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) touch:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
    NSString* message = [command.arguments objectAtIndex:0];
    
    //터치 이벤트를 처리해야합니다.
    if (message != nil && [message length] > 0)
    {
        if([message isEqualToString:@"enable"])
        {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
            [self.viewController.view setUserInteractionEnabled:YES];
        }
        else if([message isEqualToString:@"disable"])
        {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
            [self.viewController.view setUserInteractionEnabled:NO];
        }
    }
    else
    {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }
    
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) inapppurchase:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
    NSString* message = [command.arguments objectAtIndex:0];
    
    //터치 이벤트를 처리해야합니다.
    if (message != nil && [message length] > 0)
    {
        [[InAppPurchase getInstance] requestProduct:message];
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
    }
    else
    {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }
    
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}

- (void) deviceready:(CDVInvokedUrlCommand*)command
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DEVICE_READY" object:nil];
}
@end
